package com.wontube.smr.web.controller.admin;

import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.web.client.admin.IAdminNocopyHandler;
import com.wontube.smr.web.client.model.po.KeywordNocopy;
import com.wontube.smr.web.client.model.vo.KeywordNocopyItems;

/**
 * @author tanggf(2012-7-10)
 *
 */
@Controller
@RequestMapping("/admin/nocopy")
public class AdminKeywordNocopyController {
	private final static int PAGE_SIZE = 10;
	private final static String SUCCESS = "Done";
	private final static String FAILURE = "Failure: something wrong.";
	
	@RequestMapping("")
	public ModelAndView keyword_nocopy_home(){
		return new ModelAndView("admin/nocopy/nocopy_index");
	}
	
	@RequestMapping("/list")
	public ModelAndView list_keyword_nocopy(
			@RequestParam(value="page", defaultValue="1") Integer page,
			@RequestParam("keyType") String keyType){
		ModelAndView mv = new ModelAndView("admin/nocopy/nocopy_list");
		IAdminNocopyHandler handler = getNocopyHandler();
		KeywordNocopyItems items = handler.listKeywordNocopies(keyType, new PagenateInfo(page, PAGE_SIZE));
		mv.addObject("nocopyItems", items);
		return mv;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public String add_nocopy(KeywordNocopy nocopy){
		String keyword = nocopy.getKeyword();
		if(StringUtils.isNullOrEmpty(keyword)){
			return "Keyword cannot be empty";
		}
		
		nocopy.setId(UUID.randomUUID().toString());
		nocopy.setKeyword(keyword.toLowerCase());
		IAdminNocopyHandler handler = getNocopyHandler();
		Integer aff = handler.addKeywordNocopy(nocopy);
		if(aff != null && aff.intValue() == 1){
			return SUCCESS;
		}
		
		return FAILURE;
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete_nocopy(@RequestParam("id") String id){
		IAdminNocopyHandler handler = getNocopyHandler();
		Integer aff = handler.deleteKeywordNocopy(id);
		if(aff != null && aff.intValue() == 1){
			return SUCCESS;
		}
		
		return FAILURE;
	}
	
	@RequestMapping(value="/delete_cache", method=RequestMethod.GET) 
	@ResponseBody
	public String deleteCache() {
		IAdminNocopyHandler handler = getNocopyHandler();
		boolean success = handler.deleteCache();
		if(success){
			return SUCCESS;
		}
		
		return FAILURE;
	}
	
	private IAdminNocopyHandler getNocopyHandler(){
		Remoting<IAdminNocopyHandler> remoting = new Remoting<IAdminNocopyHandler>();
		return remoting.createService(IAdminNocopyHandler.class);
	}
}
